﻿#include "listpushbutton.h"
#include <QDebug>

ListPushButton::ListPushButton(QWidget *parent) : QPushButton(parent)
{
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, &QPushButton::clicked, this, &ListPushButton::showMenu);
}

void ListPushButton::showMenu(){
    menu.exec(QCursor::pos());
}

void ListPushButton::setMenuItemsLabel(QStringList labels){
    actions.clear();
    for(int i=0;i<labels.size();i++){
        QAction *action = new QAction(labels[i], this);
        action->setCheckable(true);

        connect(action, &QAction::triggered,
                [=]()
        {
            emit this->menuItemClicked(i, action->isChecked());
        });

        menu.addAction(action);
        actions.append(action);
    }
}

void ListPushButton::setCheckedState(QList<bool> checkedState){
    if(checkedState.size()!=actions.size()){
        qDebug()<<"Wrong checkState size!";
        return;
    }
    for(int i=0;i<actions.size();i++){
        actions[i]->setChecked(checkedState[i]);
    }
}
